﻿using System;
using System.Collections.Generic;
using System.Linq;
using Biz.EF.Platform.Gxt;
using log4net;

namespace Biz.Process
{
    public static class MonitorAndTotalMnMap
    {
        private static ILog Log = LogManager.GetLogger(typeof(MonitorAndTotalMnMap));
        private static List<pi_total_water_port_map> _maps;
        private static DateTime _lastLoadTime = DateTime.Now;
        public static string GetTotalMnByMonitorMn(string mn)
        {
            try
            {
                if (_maps == null || (DateTime.Now - _lastLoadTime).TotalMinutes > 10)
                {
                    using (var pCtx = new PlatformGxtContext())
                    {
                        _maps = pCtx.pi_total_water_port_map.ToList();
                    }
                    _lastLoadTime = DateTime.Now;
                }
                var item = _maps?.FirstOrDefault(t => t.SCMN.Equals(mn));
                return item?.ZLMN;
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
            return null;

        }
        public static List<string> GetTotalMns()
        {
            try
            {
                if (_maps == null || (DateTime.Now - _lastLoadTime).TotalMinutes > 10)
                {
                    using (var pCtx = new PlatformGxtContext())
                    {
                        _maps = pCtx.pi_total_water_port_map.ToList();
                    }
                    _lastLoadTime = DateTime.Now;
                }
                if (_maps != null)
                    return _maps.Select(t => t.ZLMN).ToList();
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
            return null;

        }
    }
}
